package idiet.dao.hibernate;

import idiet.bean.FoodIngredientElement;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("foodIngredientElementDao")
public class FoodIngredientElementDao extends BaseDao{
	/*@Autowired
	SessionFactory sessionFactory;
	@Autowired
	SqlStatementHelper sqlStatementHelper;*/
	public FoodIngredientElement getFoodIngredientElement(int id){
		return (FoodIngredientElement) this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredientElement.class)
				.add(Restrictions.eq("id", id))
				.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<FoodIngredientElement> getFoodIngredientElements(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredientElement.class)
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodIngredientElement> getFoodIngredientElements(String elementName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredientElement.class)
				//.add(Restrictions.like("name", elementName))
				.add(Restrictions.or(Restrictions.like("name", elementName), Restrictions.like("shortEnName", elementName)))
				.list();
	}
	public void createFoodIngredientElement(FoodIngredientElement element){
		this.sessionFactory.getCurrentSession().save(element);
	}
	public void updateFoodIngredientElement(FoodIngredientElement element){
		this.sessionFactory.getCurrentSession().update(element);
	}
	public void deleteFoodIngredientElement(FoodIngredientElement element){
		this.sessionFactory.getCurrentSession().delete(element);
	}
}
